Skip to content

feat: add websocket auto-reconnect, resubscribe, and re-auth#3

Open
harley-poly wants to merge 5 commits into
mainfrom
harley/sdk-ws-reliability
Open

feat: add websocket auto-reconnect, resubscribe, and re-auth#3
harley-poly wants to merge 5 commits into
mainfrom
harley/sdk-ws-reliability

Conversation

@harley-poly
Copy link
Copy Markdown
Member

@harley-poly harley-poly commented Jun 2, 2026

summary

makes the private and markets websockets resilient to dropped connections:

  • automatic reconnect with exponential backoff + jitter on unexpected drops (auto_reconnect=True by default; reconnect_max_attempts configurable).
  • each reconnect re-signs the auth handshake with a fresh timestamp/signature (the api authenticates only at upgrade), and replays every active subscription.
  • subscriptions are tracked on subscribe/unsubscribe so they can be replayed; a reconnect event fires after a successful reconnect.
  • reconnect stops on fatal upgrade failures (401/403/429); transient/network failures keep retrying. supports both modern and legacy websockets exception shapes.

note: per the server protocol, order/position/trade streams don't replay history on resubscribe (market-data and account-balance snapshots are re-sent automatically); use SUBSCRIPTION_TYPE_ORDER_SNAPSHOT to refetch open orders after a reconnect.

test plan

  • ruff check / ruff format --check
  • mypy polymarket_us
  • pytest (127 passed; new tests/test_websocket_reconnect.py covers subscription tracking, resubscribe replay, transient-then-success reconnect, fatal-auth stop, max-attempts, and the upgrade-status helper)

Note

Medium Risk
Changes live streaming connection semantics for authenticated trading and market feeds; missed or stale order/position state is possible if callers ignore the documented non-replay behavior after reconnect.

Overview
WebSocket clients (private and markets) now recover from unexpected disconnects by default: exponential backoff with jitter, a fresh signed upgrade on each attempt, and automatic replay of every subscription still registered via subscribe (cleared on unsubscribe). Successful recovery emits a reconnect event; auto_reconnect and reconnect_max_attempts tune behavior, and upgrade failures 401/403/429 stop retries instead of looping.

The README documents that order / position / trade streams do not replay historical updates after reconnect (snapshots for market data and balances are described as automatic; open orders may need an order snapshot subscription).

Supporting changes: close() cancels the background run/reconnect task; socket cleanup before retry avoids leaks; MarketsWebSocket / PrivateWebSocket accept broader constructor kwargs for reconnect options; auth test fix for 64-byte keys; new tests/test_websocket_reconnect.py coverage.

Reviewed by Cursor Bugbot for commit 49bd5d1. Bugbot is set up for automated code reviews on this repo. Configure here.

Comment thread polymarket_us/websocket/base.py
Comment thread polymarket_us/websocket/base.py
Comment thread polymarket_us/websocket/base.py
Comment thread polymarket_us/websocket/base.py
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 8b3f32d. Configure here.

Comment thread polymarket_us/websocket/base.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants